package com.xdf.ecommerce.controller;

import com.xdf.ecommerce.annotation.IgnoreResponseAdvice;
import com.xdf.ecommerce.service.JWTService;
import com.xdf.ecommerce.service.impl.JWTServiceImpl;
import com.xdf.ecommerce.vo.JwtToken;
import com.xdf.ecommerce.vo.UsernameAndPassword;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**
 * @author chanchaw
 * @create 2025-10-16 8:39
 */

@Slf4j
@CrossOrigin
@RestController
@RequiredArgsConstructor
@RequestMapping("/authority")
public class AuthorityController {
    private final JWTService jwtService;

    @IgnoreResponseAdvice
    @PostMapping("/token")
    public JwtToken getToken(@RequestBody UsernameAndPassword uap) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        String s = jwtService.generateToken(uap.getUsername(), uap.getPassword());
        log.info("登录后响应来的token:[{}]",s);
        return new JwtToken(s);
    }

    @IgnoreResponseAdvice
    @PostMapping("/register")
    public JwtToken register(@RequestBody UsernameAndPassword uap) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        String s = jwtService.registerAndGetToken(uap);
        return new JwtToken(s);
    }
}
